import nodemailer from 'nodemailer';

type SendEmailParams = {
  from?: string | number,
  address: string,
  title: string,
  content: string,
}

const sendEmail = async ({
  from, address, title, content,
}: SendEmailParams) => {
  // 创建传输方式
  const transporter = nodemailer.createTransport({
    host: process.env.EMAIL_HOST,
    port: Number(process.env.EMAIL_PORT),
    // true for 465, false for other ports
    secure: process.env.EMAIL_SECURE === 'true',
    auth: {
      // generated ethereal user -- 发件邮箱
      user: process.env.EMAIL_AUTH_USER,
      // generated ethereal password -- 授权码
      pass: process.env.EMAIL_AUTH_PASS,
    },
  });

  // 要发送的内容
  const sendInfo = {
    // 邮件发送人
    from: `${from || process.env.EMAIL_AUTH_FROM} <${process.env.EMAIL_AUTH_USER}>`,
    to: address,
    // 邮件标题
    subject: title,
    // 邮件正文
    html: content,
  };

  // 发送
  await transporter.sendMail(sendInfo);
};

export default sendEmail;
